package com.otdemo.service;

import com.otdemo.dao.RestClassificationDao;
import com.otdemo.dao.RestScoreDao;
import com.otdemo.dao.RestUserDao;
import com.otdemo.entity.RestClassification;
import com.otdemo.entity.RestScore;
import com.otdemo.entity.RestUser;
import com.otdemo.resp.RestUserResp;
import com.otdemo.resp.ResultResponse;
import com.otdemo.util.CacheUtil;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class RestUserService {

    public List<RestUserResp> initList() {
        List<RestUser> list = new RestUserDao().list();
        List<RestScore> scoreList = new RestScoreDao().list();
        List<RestClassification> classificationList = new RestClassificationDao().list();
        List<RestUserResp> respList = new ArrayList<>();
        try {
            int index = 1;
            for (RestUser rest : list) {
                for (RestClassification restClassification : classificationList) {
                    RestUserResp resp = new RestUserResp();
                    resp.setId(index);
                    resp.setName(rest.getName());
                    index++;
                    List<RestScore> scoreList1 = scoreList.stream().filter(p -> p.getUser().equals(rest.getName())
                            && p.getClassification().equals(restClassification.getName())).collect(Collectors.toList());
                    BigDecimal score = new BigDecimal(0.00);
                    for (RestScore restScore : scoreList1) {
                        score = score.add(restScore.getScore());
                    }
                    resp.setScore(score);
                    resp.setClassificationName(restClassification.getName());
                    respList.add(resp);
                }
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return respList;
    }

    /**
     * 新增
     *
     * @return
     */
    public ResultResponse insert(RestUser rest) {
        ResultResponse resultResponse = new ResultResponse();
        RestUser user1 = new RestUserDao().findByUserName(rest.getUsername());
        if (user1 != null) {
            return resultResponse.fail("账号已存在");
        }
        try {
            new RestUserDao().insert(rest);
        } catch (Exception e) {
            return resultResponse.fail(e.getMessage());
        }
        return resultResponse.success();
    }

    /**
     * 更新
     *
     * @return
     */
    public ResultResponse update(RestUser rest) {
        ResultResponse resultResponse = new ResultResponse();
        RestUser rest1 = new RestUserDao().findByUid(rest.getUid());
        if (rest1 == null) {
            return resultResponse.fail("查询不存在");
        }
        try {
            new RestUserDao().update(rest);
        } catch (Exception e) {
            return resultResponse.fail(e.getMessage());
        }
        return resultResponse.success();
    }

    /**
     * 查询
     *
     * @return
     */
    public ResultResponse findByUid(RestUser rest) {
        ResultResponse resultResponse = new ResultResponse();
        try {
            RestUser rest1 = new RestUserDao().findByUid(rest.getUid());
            return resultResponse.success(rest1);
        } catch (Exception e) {
            return resultResponse.fail(e.getMessage());
        }
    }

    /**
     * 列表
     *
     * @return
     */
    public ResultResponse list(RestUser rest) {
        ResultResponse resultResponse = new ResultResponse();
        try {
            List<RestUser> list = new RestUserDao().list();
            return resultResponse.success(list);
        } catch (Exception e) {
            return resultResponse.fail(e.getMessage());
        }
    }

    public ResultResponse login(RestUser user) {
        ResultResponse resultResponse = new ResultResponse();
        RestUser user1 = new RestUserDao().findByUserName(user.getUsername());
        if (user1 == null) {
            return resultResponse.fail("账号不存在");
        }
        if (!user1.getPassword().equals(user.getPassword())) {
            return resultResponse.fail("密码不正确");
        }
        CacheUtil.put(CacheUtil.USER, user1);
        return resultResponse.success(user1);
    }

    public RestUser currentUser() {
        RestUser user = (RestUser) CacheUtil.get(CacheUtil.USER);
        return user;
    }

    public String[] userList() {
        try {
            List<RestUser> list = new RestUserDao().list();
            List<String> names = list.stream().map(p -> p.getName()).collect(Collectors.toList());
            String[] strArray = new String[names.size() + 1];
            return names.toArray(strArray);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
